//
//  GoodDetailCell.m
//  jipin
//
//  Created by lcb on 5/15/15.
//  Copyright (c) 2015 liuchengbin. All rights reserved.
//

#import "GoodDetailCell.h"
#import "EXPhotoViewer.h"
#import "UIButtonView.h"
@interface GoodDetailCell ()
{
    
}
@property (nonatomic, strong) UIView *bgView;
@property(nonatomic,strong)UILabel *usernameLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)RTLabel *skuNameLabel;
@property(nonatomic,strong) UIImageView *pageScrollView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIButtonView *btnLikeV;
@property (nonatomic, strong) UIButtonView *btnReplyV;
@property(nonatomic,strong)UILabel *priceLabel;
@end
@implementation GoodDetailCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        _bgView.backgroundColor =[UIColor clearColor];
        [self.contentView addSubview:_bgView];
        //
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
        _pageScrollView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _pageScrollView.backgroundColor =UIColorFromRGB(0xffffff);
        _pageScrollView.userInteractionEnabled =YES;
        _pageScrollView.contentMode = UIViewContentModeScaleAspectFit;
        [_pageScrollView addGestureRecognizer:singleRecognizer];
        [_bgView addSubview:_pageScrollView];
        singleRecognizer=nil;
        //
        _iconView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _iconView.image =[UIImage imageNamed:@"user_face.png"];
        _iconView.backgroundColor = PAILIDE_WHITE_FRAME_COLOR;
        _iconView.alpha = ALPHA_BACKVIEW_PAILIDE;
        _iconView.layer.borderWidth = 2;
        _iconView.layer.borderColor = [[UIColor whiteColor] CGColor];
        [_bgView addSubview:_iconView];
        
        _usernameLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        _usernameLabel.backgroundColor = [UIColor clearColor];
        _usernameLabel.textColor = UIColorFromRGB(0x285790);
        _usernameLabel.textAlignment = NSTextAlignmentLeft;
        _usernameLabel.font = FONT(14);
        [_bgView addSubview:_usernameLabel];
        _timeLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.textColor = [UIColor darkGrayColor];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = FONT(14);
        [_bgView addSubview:_timeLabel];
        
        _skuNameLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, 0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, 0)];
        _skuNameLabel.backgroundColor = [UIColor clearColor];
        _skuNameLabel.textColor = [UIColor darkGrayColor];
        _skuNameLabel.textAlignment = NSTextAlignmentLeft;
        _skuNameLabel.font = FONT(14);
        _skuNameLabel.lineSpacing = LSpacing;
        [_bgView addSubview:_skuNameLabel];
        //
        
        //
        _btnLikeV = [[UIButtonView alloc] initWithFrame:CGRectZero];
        _btnLikeV.backgroundColor =  [UIColor clearColor];
        __weak typeof (self) wSelf = self;
        [_btnLikeV setCallBack:^(BEventType t, UIView *v, id data, id key) {
            [wSelf likeAction:nil];
        }];
        [_bgView addSubview:_btnLikeV];
        
        _btnReplyV = [[UIButtonView alloc] initWithFrame:CGRectZero];
        _btnReplyV.backgroundColor =  [UIColor clearColor];
        [_btnReplyV setCallBack:^(BEventType t, UIView *v, id data, id key) {
            [wSelf commentAction:nil];
        }];
        [_bgView addSubview:_btnReplyV];
        
        _priceLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.textColor = [UIColor darkGrayColor];
        _priceLabel.textAlignment = NSTextAlignmentRight;
        _priceLabel.font = FONT(12);
        [_bgView addSubview:_priceLabel];

    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.bgView=nil;
    self.usernameLabel=nil;
    self.timeLabel=nil;
    self.skuNameLabel=nil;
    self.pageScrollView.image=nil;
    self.pageScrollView=nil;
    self.iconView.image=nil;
    self.iconView=nil;
    self.btnLikeV=nil;
    self.btnReplyV=nil;
    self.priceLabel=nil;
//    DLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _bgView.frame =CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    self.indexPath=indexPath;
    float fTop = 0;

     NSString * strURL = [[NSString stringWithFormat:@"%@", [dict objectForKey:@"largePicURL"]] replaceNullString];
    _pageScrollView.frame =CGRectMake(PHOTO_FRAME_WIDTH, fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2 , DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2);
    [_pageScrollView sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:[UIImage imageNamed:@"image.png"]];

    //
    fTop = _pageScrollView.frame.origin.y+_pageScrollView.frame.size.height;
    NSString *iconstr =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"largeFace"]] replaceNullString];
    if(iconstr && iconstr.length>0&& ![iconstr isEqualToString:@"(null)"])
    {
        fTop = fTop - (43/2);
        _iconView.frame = CGRectMake(PHOTO_FRAME_WIDTH, fTop ,  43, 43);
        [_iconView sd_setImageWithURL:[NSURL URLWithString:iconstr] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
        fTop=_iconView.frame.origin.y+_iconView.frame.size.height;
    }
    NSString *username =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"nick_name"]] replaceNullString];
    if (username && username.length>0&& ![username isEqualToString:@"(null)"]) {
        if (_iconView.frame.origin.x>0) {
            _usernameLabel.frame = CGRectMake(_iconView.frame.origin.x+_iconView.frame.size.width+5,fTop-12, 120, 12);
        }
        else{
            _usernameLabel.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, 120, 12);
        }
        _usernameLabel.text =username;
    }
    NSString *timestr =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"upload_dateinfo"]] replaceNullString];
    if (timestr && timestr.length>0 && ![timestr isEqualToString:@"(null)"]) {
        if (_iconView.frame.origin.x>0) {
            _timeLabel.frame = CGRectMake(DEVICE_WIDTH-100-PHOTO_FRAME_WIDTH*2,fTop-12, 100, 12);
        }else{
            _timeLabel.frame = CGRectMake(DEVICE_WIDTH-100-PHOTO_FRAME_WIDTH*2,fTop, 100, 12);
        }
        
        _timeLabel.text =timestr;
    }
    if (_iconView.frame.origin.x<=0 && ((username.length>0&& ![username isEqualToString:@"(null)"])||(timestr.length>0&& ![timestr isEqualToString:@"(null)"]))) {
        fTop=fTop+12;
    }
    fTop = fTop+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    
    NSString *test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (test.length==0) {
        test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    }
    if (test.length>0) {
        test =[NSString stringWithFormat:@"“%@”",test];
    }
    [_skuNameLabel setText:test];
    CGSize optimumSize = [_skuNameLabel optimumSize];
    _skuNameLabel.frame = CGRectMake(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING,fTop, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2, optimumSize.height);
    
    //
    fTop = _skuNameLabel.frame.origin.y+optimumSize.height+PHOTO_FRAME_WIDTH;
    NSString *reply_cnt =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"reply_cnt"]] replaceNullString];
    if (reply_cnt.length>0&&![reply_cnt isEqualToString:@"0"]) {
        [_btnReplyV setImage:[UIImage imageNamed:@"pinglun_icon.png"] name:reply_cnt];
        
    }else{
        reply_cnt=@"点评";
        [_btnReplyV setImage:[UIImage imageNamed:@"pinglun_icon.png"] name:@"点评"];
        
    }
    CGSize size1=[reply_cnt sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0]}];
    [_btnReplyV setFrame:CGRectMake(PAD_LEFT ,fTop, size1.width+25, MORE_BUTTON_HEIGHT)];
    
    NSString *like_cnt =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"like_cnt"]] replaceNullString];
    if (like_cnt.length>0&&![like_cnt isEqualToString:@"0"]) {
        [_btnLikeV setImage:[UIImage imageNamed:@"like_icon.png"] name:like_cnt];
    }else
    {
        like_cnt=@"喜欢";
        [_btnLikeV setImage:[UIImage imageNamed:@"like_icon.png"] name:@"喜欢"];
    }
    CGSize size2=[like_cnt sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0]}];
    [_btnLikeV setFrame:CGRectMake(_btnReplyV.frame.origin.x+_btnReplyV.frame.size.width+PHOTO_FRAME_WIDTH ,fTop, size2.width+25, MORE_BUTTON_HEIGHT)];
    
    int isLike=[[dict objectForKey:@"like"] intValue];
    if (isLike) {
        [_btnLikeV setImage:[UIImage imageNamed:@"like_icon_selected.png"] name:like_cnt];
        
    }
    

    _priceLabel.frame = CGRectMake(DEVICE_WIDTH-82-PHOTO_FRAME_WIDTH*2,fTop, 90, MORE_BUTTON_HEIGHT);
    _priceLabel.text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"price_str"]] replaceNullString];
    [self layoutSubviews];
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    NSString *test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (test.length==0) {
        test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    }
    if (test.length>0) {
        test =[NSString stringWithFormat:@"“%@”",test];
    }
    RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*2,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4,0)];
    textLabel.font = FONT(14);
    textLabel.lineSpacing = LSpacing;
    [textLabel setText:test];
    CGSize optimumSize1 = [textLabel optimumSize];
     textLabel=nil;
    int height2 = optimumSize1.height;
    
    NSString *iconstr =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"largeFace"]] replaceNullString];
    NSString *username =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"nick_name"]] replaceNullString];
    NSString *timestr =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"upload_dateinfo"]] replaceNullString];
    if (iconstr && iconstr.length>0 && ![iconstr isEqualToString:@"(null)"]) {
        return (DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2)+height2+MORE_BUTTON_HEIGHT+PHOTO_FRAME_WIDTH*2+(43/2);
    }else if( username.length==0 && timestr.length==0){
        return (DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2)+height2+MORE_BUTTON_HEIGHT+PHOTO_FRAME_WIDTH*2;
    }else
    {
        return (DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2)+height2+MORE_BUTTON_HEIGHT+PHOTO_FRAME_WIDTH*3;
    }
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)likeAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_like,sender,self.data,nil,self.indexPath);
    }
}
-(void)commentAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_comment,sender,self.data,nil,self.indexPath);
    }
}
-(void)handleSingleTapFrom
{
    [EXPhotoViewer showImageFrom:_pageScrollView];
}

@end
